package calculatorAircraftFileReader;
import java.awt.*;
import java.util.List;

import javax.swing.JApplet;

import java.io.IOException;
import java.util.ArrayList;

import calculatorEngine.Seat;

public class AircraftFileVariables {

	int rowsNumber;
	int realColumnsNumber;
	int columnsNumber;
	int optimalWeightCenter;
	int numberOfLines;
	String howManyColumns;
	String MapOfColumns;
	public String name;
	String howManyRows;
	String readWeightCenter;
	String aircraftFilePath;
	int[] empty;
	int u = 5;
	public String readLines[];
	String h = "b";
	int b = -1;
	int x, y, e = 0;
	int a = 4;
	Seat[][] testArrayOfSeats;
	int n;
	ArrayList<Seat> testArrayList = new ArrayList<Seat>();
	public String path;

	// Mikolaj 01.12.13 22:23
	// kazda metoda odpowiada za kazda zmienna. Pozniej stworzycie sobie obiekt
	// ktory te zmienne bedzie wam zwracal :)

	public AircraftFileVariables(String path) {
		this.aircraftFilePath = path;
		try {
			ReadAircraftFile file = new ReadAircraftFile(this.aircraftFilePath);
			readLines = file.OpenFile();
			numberOfLines = file.readLines();
		} catch (IOException m) {
			System.out.println(m.getMessage());
		}

	}
	public AircraftFileVariables(){
		 this.aircraftOptimalWeightCenter();
		 System.out.println(optimalWeightCenter);
	}

	// Nazwa samolotu
	public String aircraftName() {

		name = readLines[0];

		return name;
	}

	// Ilosc rzedow
	public int aircraftRowsNumber() {

		howManyRows = readLines[1];

		rowsNumber = Integer.parseInt(howManyRows);
		return rowsNumber;
	}

	// PRAWDZIWA ilosc kolumn (z uwzglednieniem pustych)
	public int aircraftColumnsNumber() {

		MapOfColumns = readLines[3];

		realColumnsNumber = MapOfColumns.length();
		return realColumnsNumber;
	}
	
	public int aircraftCompare(String l){
		if(l.equals("X") == false){
		int m = 1;
		while (l.equals(MapOfColumns.substring(m - 1, m)) == false){
			m++;}
		return m;}
		else { return 0;}
		
	}

	// optymalny srodek ciezkosci
	public int aircraftOptimalWeightCenter() {

		readWeightCenter = readLines[4];

		optimalWeightCenter = Integer.parseInt(readWeightCenter);
		return this.optimalWeightCenter;
	}

	// tablica z danymi na temat pustakow
	public int[] aircraftEmptyColumnsArray() {

		howManyColumns = readLines[2];
		MapOfColumns = readLines[3];

		columnsNumber = Integer.parseInt(howManyColumns);
		Columns seatColumns = new Columns(MapOfColumns, columnsNumber);
		int[] empty = seatColumns.pustaki();
		/*
		 * Do sprawdzenia czy tablica dziala, u mnie dziala :)
		 * 
		 * for(int i=0; i< empty.length; i++){
		 * 
		 * System.out.println(empty[i]); }
		 */
		return empty;
	}

	/*public Seat[][] aircraftSeatMap() {

		// pierwszy string, name, nie musi byc modyfikowany
		name = readLines[0];

		// ilosc wierszy
		howManyRows = readLines[1];

		// ilosc kolumn UMOZLIWIAJACYCH ZAJECIE SIEDZENIA
		howManyColumns = readLines[2];

		// lista PRAWDZIWEJ ilosci kolumn, przetwarzana w klasie columns
		MapOfColumns = readLines[3];

		rowsNumber = Integer.parseInt(howManyRows);
		columnsNumber = Integer.parseInt(howManyColumns);
		Columns seatColumns = new Columns(MapOfColumns, columnsNumber);
		int[] empty = seatColumns.pustaki();

		testArrayOfSeats = new Seat[rowsNumber][MapOfColumns.length()];
		List<Object> testArrayList = new ArrayList<Object>();

		// Moglbys zamiast booleanow uzyc obiektu Seat, ktory stworzylem w
		// folderze calculatorEngine?
		// Pawel, 07.12.2013, 23:09

		
		 *  *************UWAGA************* Zupelnie nowa wersja algorytmu. Liczy
		 * wszystko, dziala pieknie, jak na razie tylko wypisuje wszystkie
		 * siedzenia, jutro zalatwie sprawe zeby wpisywal to do obiektu JEDYNYM
		 * problemem tego algorytmu jest to ze jest ograniczona liczba KORYTARZY
		 * w ilosci 5 zawsze ja mozna zwiekszyc dodajac nowe zmienne
		 * corridor...+1 Mikolaj, 11.12.2013, 01:01
		 
		int corridorOne = empty[e];
		int corridorTwo = -1;
		int corridorThree = -1;
		int corridorFour = -1;
		int corridorFive = -1;
		try {
			corridorTwo = empty[e + 1];
		} catch (ArrayIndexOutOfBoundsException v) {

		}
		try {
			corridorThree = empty[e + 2];
		} catch (ArrayIndexOutOfBoundsException v) {

		}
		try {
			corridorFour = empty[e + 3];
		} catch (ArrayIndexOutOfBoundsException v) {

		}
		try {
			corridorFive = empty[e + 4];
		} catch (ArrayIndexOutOfBoundsException v) {

		}

		for (int u = 0; u < (MapOfColumns.length() * rowsNumber); u++) {

			b++;
			if (b != corridorOne) {
				if (b != corridorTwo) {
					if (b != corridorThree) {
						if (b != corridorFour) {
							if (b != corridorFive) {

								if (a < numberOfLines - 1) {
									a++;
									if (readLines[a].substring(4, 5).equals(h) || readLines[a].substring(5, 6).equals(h) || readLines[a].substring(6, 7).equals(h)) {

										// aaaaaaaaaaaaaaaaaaaa
										
										testArrayOfSeats[x][y] = new Seat(2, 5);
										if (y < MapOfColumns.length() - 1) {
											y++;
										} else {
											x++;
											y = 0;
										}

									} else {

										// aaaaaaaaaaaaaaaaaaaaa aaa aa
										
										testArrayOfSeats[x][y] = new Seat(2, 4);
										if (y < MapOfColumns.length() - 1) {
											y++;
										} else {
											x++;
											y = 0;
										}

									}
								}
							} else {
								corridorFive += MapOfColumns.length();
								
								testArrayOfSeats[x][y] = new Seat(2, 3);
								if (y < MapOfColumns.length() - 1) {
									y++;
								} else {
									x++;
									y = 0;
								}
							}
						}

						else {
							corridorFour += MapOfColumns.length();
							
							testArrayOfSeats[x][y] = new Seat(2, 3);
							if (y < MapOfColumns.length() - 1) {
								y++;
							} else {
								x++;
								y = 0;
							}
						}
					}

					else {
						corridorThree += MapOfColumns.length();
						
						testArrayOfSeats[x][y] = new Seat(2, 3);
						if (y < MapOfColumns.length() - 1) {
							y++;
						} else {
							x++;
							y = 0;
						}
					}
				}

				else {
					corridorTwo += MapOfColumns.length();
					
					testArrayOfSeats[x][y] = new Seat(2, 3);
					if (y < MapOfColumns.length() - 1) {
						y++;
					} else {
						x++;
						y = 0;
					}
				}
			}

			else {
				corridorOne += MapOfColumns.length();
				
				testArrayOfSeats[x][y] = new Seat(2, 3);
				if (y < MapOfColumns.length() - 1) {
					y++;
				} else {
					x++;
					y = 0;
				}
			}
		}

		return testArrayOfSeats;
	}
	*/
 public ArrayList<Seat> aircraftTestList(){
	// pierwszy string, name, nie musi byc modyfikowany
			name = readLines[0];

			// ilosc wierszy
			howManyRows = readLines[1];

			// ilosc kolumn UMOZLIWIAJACYCH ZAJECIE SIEDZENIA
			howManyColumns = readLines[2];

			// lista PRAWDZIWEJ ilosci kolumn, przetwarzana w klasie columns
			MapOfColumns = readLines[3];

			rowsNumber = Integer.parseInt(howManyRows);
			columnsNumber = Integer.parseInt(howManyColumns);
			Columns seatColumns = new Columns(MapOfColumns, columnsNumber);
			int[] empty = seatColumns.pustaki();

			testArrayOfSeats = new Seat[rowsNumber][MapOfColumns.length()];
			

			// Moglbys zamiast booleanow uzyc obiektu Seat, ktory stworzylem w
			// folderze calculatorEngine?
			// Pawel, 07.12.2013, 23:09

			/*
			 *  *************UWAGA************* Zupelnie nowa wersja algorytmu. Liczy
			 * wszystko, dziala pieknie, jak na razie tylko wypisuje wszystkie
			 * siedzenia, jutro zalatwie sprawe zeby wpisywal to do obiektu JEDYNYM
			 * problemem tego algorytmu jest to ze jest ograniczona liczba KORYTARZY
			 * w ilosci 5 zawsze ja mozna zwiekszyc dodajac nowe zmienne
			 * corridor...+1 Mikolaj, 11.12.2013, 01:01
			 */
			int corridorOne = empty[e];
			int corridorTwo = -1;
			int corridorThree = -1;
			int corridorFour = -1;
			int corridorFive = -1;
			try {
				corridorTwo = empty[e + 1];
			} catch (ArrayIndexOutOfBoundsException v) {

			}
			try {
				corridorThree = empty[e + 2];
			} catch (ArrayIndexOutOfBoundsException v) {

			}
			try {
				corridorFour = empty[e + 3];
			} catch (ArrayIndexOutOfBoundsException v) {

			}
			try {
				corridorFive = empty[e + 4];
			} catch (ArrayIndexOutOfBoundsException v) {

			}

			for (int u = 0; u < (MapOfColumns.length() * rowsNumber); u++) {

				b++;
				if (b != corridorOne) {
					if (b != corridorTwo) {
						if (b != corridorThree) {
							if (b != corridorFour) {
								if (b != corridorFive) {

									if (a < numberOfLines - 1) {
										a++;
										if (readLines[a].endsWith(h)) {

											// aaaaaaaaaaaaaaaaaaaa
											testArrayList.add(new Seat(Seat.FREE, Seat.BUSINESS_CLASS));
											
									

										} else {

											// aaaaaaaaaaaaaaaaaaaaa aaa aa
											testArrayList.add(new Seat(Seat.FREE, Seat.ECONOMIC_CLASS));
											
									

										}
									}
								} else {
									corridorFive += MapOfColumns.length();
									testArrayList.add(new Seat(Seat.FREE, Seat.OUT_OF_ORDER));
								
								}
							}

							else {
								corridorFour += MapOfColumns.length();
								testArrayList.add(new Seat(Seat.FREE, Seat.OUT_OF_ORDER));
								testArrayOfSeats[x][y] = new Seat(Seat.FREE, Seat.OUT_OF_ORDER);
							}
						}

						else {
							corridorThree += MapOfColumns.length();
							testArrayList.add(new Seat(Seat.FREE, Seat.OUT_OF_ORDER));
						
						}
					}

					else {
						corridorTwo += MapOfColumns.length();
						testArrayList.add(new Seat(Seat.FREE, Seat.OUT_OF_ORDER));
						
					}
				}

				else {
					corridorOne += MapOfColumns.length();
					testArrayList.add(new Seat(Seat.FREE, Seat.OUT_OF_ORDER));
				
				}
			}
			
			String af = readLines[3];
			char charArray[]  =  af.toCharArray();
			char columnStr;
			for (int i = 0; i < testArrayList.size(); i++){
				int rowNo =i/ charArray.length;
				int d = (i+1)% charArray.length;
				if(d != 0){
				columnStr = charArray [d - 1];}
				else { columnStr = charArray [ charArray.length - 1];}
			
				testArrayList.get(i).setRowNo(rowNo + 1);
				testArrayList.get(i).setColumnStr(columnStr);
			}
			for (int i =0; i < testArrayList.size(); i++){
				if( String.valueOf(testArrayList.get(i).getColumnStr()).equals(readLines[3].substring(0, 1))){
					testArrayList.get(i).setPreferences("W");}
				else if ( readLines[3].endsWith(String.valueOf(testArrayList.get(i).getColumnStr()))){
					testArrayList.get(i).setPreferences("W");}
				else if(i != testArrayList.size() - 1){ 
					if(testArrayList.get(i+1).getComfortClass() == 3) {
				
					testArrayList.get(i).setPreferences("A");}
				else if(i != 0){ 
					if (testArrayList.get(i - 1).getComfortClass() == 3){
						testArrayList.get(i).setPreferences("A");}
					else {
						testArrayList.get(i).setPreferences("M");}
					}
					
					
				}
			}
			
			
			
			return testArrayList;
		}
 
 
 

 
 }
